import {
  imageUrl
} from "../utils/imageutil"
import {
  uploadToQiniu
} from "../utils/uploadToQiniu"
Page({

  data: {
    imageUrl,
    show: false,
    id: '',
    img: '',
    vehicle_arr: [],
    vehicle_index: -1,
    vehicle_detail_arr: [],
    vehicle_detail_index: -1,
    color_arr: [],
    color_index: -1,
    code: ''
  },

  onLoad(options) {
    if (options.id) {
      this.setData({
        id: options.id
      })
      this.user_vehicle_detail()
    } else {
      this.vehicle()
    }
  },

  user_vehicle_detail() {
    getApp().globalData.posts('/index.php/api/app/user/sorting/ouyoung_api/user_vehicle_detail?id=' + this.data.id).then(res => {
      let data = res.data.data
      this.setData({
        img:data.img,
        code:data.code,
      })
      this.vehicle().then(()=>{
        this.data.vehicle_arr.forEach((v,i)=>{
          if(v.name==data.brand){
            this.setData({
              vehicle_index:i
            })
          }
        })
        this.vehicle_detail().then(()=>{
          this.data.color_arr.forEach((v,i)=>{
            if(v==data.color){
              this.setData({
                color_index:i
              })
            }
          })
          this.data.vehicle_detail_arr.forEach((v,i)=>{
            if(v.name==data.type){
              this.setData({
                vehicle_detail_index:i
              })
            }
          })
        })
      })
    })
  },

  vehicle() {
    return new Promise((reslove, reject) => {
      getApp().globalData.posts('/index.php/api/app/user/sorting/ouyoung_api/vehicle').then(res => {
        this.setData({
          vehicle_arr: res.data.data
        })
        reslove()
      })
    })
  },
  vehicle_detail() {
    return new Promise((reslove, reject) => {
      getApp().globalData.posts('/index.php/api/app/user/sorting/ouyoung_api/vehicle_detail?id=' + this.data.vehicle_arr[this.data.vehicle_index].id).then(res => {
        this.setData({
          vehicle_detail_arr: res.data.data.type,
          color_arr: res.data.data.color,
        })
        reslove()
      })
    })

  },

  change_img() {
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        uploadToQiniu(res.tempFiles[0].tempFilePath).then(img => {
          this.setData({
            img
          })
        })
      }
    });
  },
  change_vehicle(e) {
    if (this.data.vehicle_index == e.detail.value) {
      return
    }
    this.setData({
      vehicle_index: e.detail.value,
      vehicle_detail_index: -1,
      color_index: -1,
    })
    this.vehicle_detail()
  },
  change_is_vehicle() {
    if (this.data.vehicle_index == -1) {
      getApp().showToast('请选择爱车型号')
    }
  },
  change_color(e) {
    this.setData({
      color_index: e.detail.value
    })
  },
  change_vehicle_detail(e) {
    this.setData({
      vehicle_detail_index: e.detail.value
    })
  },


  change_show() {
    var carnum = this.selectComponent('#carnum')
    carnum.change_curInput(0)
    this.setData({
      show: true
    })
  },

  get_car_num(e) {
    this.setData({
      code: e.detail
    })
  },

  bottomTap() {
    if (this.data.vehicle_index == -1) {
      getApp().showToast('请选择爱车型号')
      return
    }
    if (this.data.color_index == -1) {
      getApp().showToast('请选择爱车颜色')
      return
    }
    if (this.data.vehicle_detail_index == -1) {
      getApp().showToast('请选择爱车种类')
      return
    }
    if (!this.data.code) {
      getApp().showToast('请输入车牌号')
      return
    }
    if(!this.data.id){
      this.user_addvehicle()
    }else{
      this.user_updatevehicle()
    }
  },

  user_addvehicle() {
    getApp().globalData.posts('/index.php/api/app/user/sorting/ouyoung_api/user_addvehicle', {
      img: this.data.img,
      brand_id: this.data.vehicle_arr[this.data.vehicle_index].id,
      type_id: this.data.vehicle_detail_arr[this.data.vehicle_detail_index].id,
      color: this.data.color_arr[this.data.color_index],
      code: this.data.code
    }).then(res => {
      getApp().showModal(res.data.msg).then(() => {
        if (res.data.code == 1) {
          wx.navigateBack({
            delta: 1,
          })
        }
      })
    })
  },

  user_updatevehicle(){
    getApp().globalData.posts('/index.php/api/app/user/sorting/ouyoung_api/user_updatevehicle', {
      id:this.data.id,
      img: this.data.img,
      brand_id: this.data.vehicle_arr[this.data.vehicle_index].id,
      type_id: this.data.vehicle_detail_arr[this.data.vehicle_detail_index].id,
      color: this.data.color_arr[this.data.color_index],
      code: this.data.code
    }).then(res => {
      getApp().showModal(res.data.msg).then(() => {
        if (res.data.code == 1) {
          wx.navigateBack({
            delta: 1,
          })
        }
      })
    })
  },

})